/**
 * EASYTOGO JS UI 组件管理器，用来管理页面上的JS UI 组件。
 * 所管理的组件是通过DOM元素的 JSObjName 属性来识别，凡是具有此属性的，都将纳入 vsObjsMgr 管理器中管理。
 * @author zhaojh
 */
uiManager = new VSObjsMgr();
var easytogo_theme = readCookie("WEBSTYLE");
var easytogo_locale = readCookie("LOCALE");
function VSObjsMgr(){
	
	this.objCollection = new Array();//存放当前页面中EASYTOGO的JS UI组件
	this.tabs = new Array();
	var that = this;	
    jQuery(document).ready(function(){
    	that.loadJS("easyui",easytogo_basepath+"easytogo/baseui/easyloader.js");
		//基础标签语言、皮肤、工程路径
		if(easyloader){
			//默认皮肤
			if(easytogo_theme){
				easyloader.theme=easytogo_theme;
			} else {
				easyloader.theme = "default";
			}
			
			//默认语言en、zh_CN
			if(easytogo_locale){
				easyloader.locale = easytogo_locale;
			} else {
				easyloader.locale = "zh_CN";
			}
			//框架绝对路径
			easyloader.base=easytogo_basepath+"easytogo/baseui/";
			
			//首页样式切换
            that.loadCSS("styles",easytogo_basepath + "easytogo/webstyles/"+easyloader.theme+"/style/css.css");
		}
		
        //查找表单LOAFForm下所有包含cname属性的输入框(包括input和 textArea标签)
        var $vsObjs = jQuery("form[JSObjName],input[JSObjName],div[JSObjName],a[JSObjName],table[JSObjName]");
        that.onReady($vsObjs);
        /*
		using('tabs', function(){
			for(var i = 0,j = that.tabs.length;i < j; i++){
				that.tabs[i].onReady();
				//将含有id属性的Tabs组件放入uiManager中管理
                if(jQuery(that.tabs[i].editor).attr("id")){
                    that.objCollection[jQuery(that.tabs[i].editor).attr("id")] = that.tabs[i];
                }				
			}
		});	
        */        
    });	

}

VSObjsMgr.prototype.onReady = function($vsObjs){
	var that = this;
    $vsObjs.each(function(){
       var objName = jQuery(this).attr("JSObjName");
       switch(objName){
          case "TREE" :
				//动态加载CSS文件
				that.loadCSS("menuCss",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/css/dhtmlx_menu.css");
				that.loadCSS("treeCss",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/css/dhtmlx_tree.css");
				//动态加载JS文件
				that.loadJS("treejs",easytogo_basepath + "easytogo/ui/TreeObj.js");
				that.loadJS("dhtmlxcommon",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/js/dhtmlxcommon.js");
				that.loadJS("dhtmlxtree_full",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/js/dhtmlxtree_full.js");
				that.loadJS("dhtmlxmenu_full",easytogo_basepath + "easytogo/ui/tree/dhtmlxtree/js/dhtmlxmenu_full.js");		
				//构建UI组件
				var o = new TreeObj(this); 
				break;
          case "DATE" : 
				that.loadJS("DatePicker_js",easytogo_basepath + "easytogo/ui/DatePickerObj.js");
				that.asyncLoadJS("WdatePicker",easytogo_basepath + "easytogo/ui/date/datepicker/WdatePicker.js");
				var o = new DatePickerObj(this); 
				break;
	      case "TABS":
	            that.loadJS("tabsjs",easytogo_basepath + "easytogo/ui/TabsObj.js");
	            that.loadCSS("icons",easytogo_basepath + "easytogo/baseui/themes/icon.css");
				//easyloader.load("tabs");
				//var o1 = new TabsObj(this);
				//that.tabs.push(o1);
                var o = new TabsObj(this);
				break;
		  case "FORM":
				//加载对象js和css
				that.loadJS("formjs",easytogo_basepath + "easytogo/ui/FormObj.js");
				that.loadJS("js_validatebox",easytogo_basepath+"easytogo/baseui/plugins/jquery.validatebox.js");
				that.loadJS("locale_"+easyloader.locale,easytogo_basepath+"easytogo/baseui/locale/"+easyloader.locales[easyloader.locale]);
				that.loadCSS("style_validatebox",easytogo_basepath + "easytogo/baseui/themes/"+easyloader.theme+"/validatebox.css");
				var o = new FormObj(this);
				break;	  
		  case "layout":
				//加载对象js
				that.loadJS("layoutjs",easytogo_basepath + "easytogo/ui/Layout.js");
				var o = new Layout(this);
				break;	  	  
		  case "CHECKBOXORRADIOGROUP":
				that.loadJS("groupjs",easytogo_basepath + "easytogo/ui/CheckBoxOrRadioGroupObj.js");	  
				var o = new CheckBoxOrRadioGroupObj(this);
				break;
          case "ASSOCIATESELECT":
				that.loadJS("associatesel",easytogo_basepath + "easytogo/ui/select/associate/js/associateSel.js");
				var o = new AssoSelObj(this);
				break;
          case "PULLPUSHSELECT":
				that.loadJS("pullpushsel", easytogo_basepath+"easytogo/ui/select/pullpush/js/pullpush.js");
				var o = new PullPushSel(this);
				break;
          case "ACCORDION":
				//加载对象js和css
                that.loadJS("accordion_js",easytogo_basepath + "easytogo/ui/AccordionObj.js");
                var o = new AccordionObj(this);
				break;	
          case "LINKBUTTON":
				//加载对象js和css
                that.loadCSS("icons",easytogo_basepath + "easytogo/baseui/themes/icon.css");
				that.loadJS("linkButton",easytogo_basepath + "easytogo/ui/LinkButton.js");
				var o = new LinkButton(this);
				break;
		  case "paginationGrid":
				//加载对象js和css
				that.loadJS("paginationGrid",easytogo_basepath + "easytogo/ui/PaginationGrid.js");
				var o = new PaginationGrid(this);  
				break;
		  case "dataGrid":
				//加载对象js和css
				that.loadJS("dataGrid",easytogo_basepath + "easytogo/ui/DataGrid.js");
				that.asyncLoadJS("WdatePicker",easytogo_basepath + "easytogo/ui/date/datepicker/WdatePicker.js");
				var o = new DataGrid(this);  
				break;
		  case "filetext":
				//加载对象js和css
				that.loadJS("filetextJs",easytogo_basepath + "easytogo/ui/FileText.js");
				var o  = new FileText(this);
				break;
          case "COMBOBOX":
              that.loadJS("vscombobox", easytogo_basepath+"easytogo/ui/Combobox.js");
              var o = new Combobox(this);
              break;
          case "SPINNER":
              that.loadCSS("vsspinnercss",easytogo_basepath + "easytogo/ui/spinner/css/jquery.spinbox.css");
              that.loadJS("vsspinnerboxjs", easytogo_basepath+"easytogo/ui/spinner/js/jquery.spinbox.js");
              that.loadJS("vsspinner", easytogo_basepath+"easytogo/ui/Spinner.js");
              var o = new Spinner(this);
              break;
          case "SIMPLEPAGINATION":
              that.loadJS("simplePagination", easytogo_basepath+"easytogo/ui/SimplePagination.js");
              break;    
       }
       
       if(o){
          //每个js ui 组件都要实现onReady方法
          o.onReady(); 
          //将js ui 的 id 做为数组的下标，来存储js ui 组件。 没有设置id的不做管理
		  if(jQuery(this).attr("id")){
		  	 that.objCollection[jQuery(this).attr("id")] = o;
		  }
		  
          
       }
    });	
}

/**
 * 根据ID取得LOAFAP的自定义JS对象
 * @param {} n 
 * @return {}
 */
VSObjsMgr.prototype.getObject = function(n){
	return this.objCollection[n];
}

/**
 * 同步动态加载JS文件
 * @param {} id
 * @param {} url
 */
VSObjsMgr.prototype.loadJS = function(id,url){
	//判断指定ID的js文件是否存在，存在则不再加载此文件
    var scriptTag = document.getElementById(id);
    if(scriptTag) return;
    //同步加载js文件
	jQuery.ajax({
	    type: "POST",
	    async: false,
	    url: url,
	    dataType: "script",
	    success: function(data){
            var oHead = document.getElementsByTagName("head").item(0);
            var oScript = document.createElement("script");
            oScript.id = id;
            oScript.type = "text/javascript";
            oScript.defer = true;
            oScript.text = data;
            oHead.appendChild(oScript);
	    }
	});
}

/**
 * 异步动态加载JS文件
 * @param {} id
 * @param {} url
 */
VSObjsMgr.prototype.asyncLoadJS = function(id,url){
	//判断指定ID的js文件是否存在，存在则不再加载此文件
    var scriptTag = document.getElementById(id);
    if(scriptTag) return;
    
    //取得<head> Dom元素
    var oHead = document.getElementsByTagName("head").item(0);
    //创建<script>元素，并设置个项属性
    var oScript = document.createElement("script");
    oScript.id = id;
    oScript.type = "text/javascript";
    oScript.src = url;
    
    //将<script>元素追加到<head>元素中
    oHead.appendChild(oScript); 	
}

/**
 * 动态加载css样式文件
 * @param {} id 样式文件id,用来标识一个css文件惟一
 * @param {} url css文件的url地址
 */
VSObjsMgr.prototype.loadCSS = function(id,url){
	//判断指定ID的CSS文件是否存在，存在则不再加载此文件
    var cssTag = document.getElementById(id);
    if(cssTag) return;
    
    var cssTag = document.getElementById(id);
    var oHead = document.getElementsByTagName("head").item(0);
    var oCss = document.createElement("link");
    oCss.id = id;
    oCss.rel = "stylesheet";
    oCss.type = "text/css";
    oCss.href = url;
    oHead.appendChild(oCss); 
}

